# from datetime import datetime
#
# from sqlalchemy import func, select, exc, and_
# from app.config.db import DBUtils
# from app.model.FKListModel import fklist
# from app.model.SKListModel import sklist
# from app.model.actualcontrollerModel import ActualController
# from app.model.CompanyPositionModel import CompanyPositionModel
#
# class TradeCommunityService:
#     def __init__(self):
#         self.db_utils = DBUtils()
#         self.current_year = datetime.now().year
#
#     def select_trade_community_info(self, session, company, is_cydw):
#         if int(is_cydw) == 1:
#             try:
#                 wldw_sub = select(fklist.SKZHMC).where(fklist.FKZHMC == company).distinct()
#                 wldw_list = select(ActualController.GSMC).where(ActualController.NAME.in_(wldw_sub))
#                 comm_fk_query = select(fklist.FKZHMC,
#                                        fklist.SKZHMC,
#                                        func.sum(fklist.FKJE).label('total') \
#                                        .where(fklist.SKZHMC.in_(wldw_list)) \
#                                        .group_by(fklist.SKZHMC, fklist.FKZHMC)
#                                        )
#                 comm_sk_query = select(sklist.FKZHMC,
#                                        sklist.SKZHMC,
#                                        func.sum(sklist.FKJE).label('total') \
#                                        .where(sklist.FKZHMC.in_(wldw_list)) \
#                                        .group_by(sklist.FKZHMC, sklist.SKZHMC)
#                                        )
#             except exc.SQLAlchemyError as e:
#                 error_response = [{'error': str(e)}]
#                 try:
#                     self.db_utils.rollback()
#                 except Exception as rollback_error:
#                     print(f"Failed to rollback transaction: {rollback_error}")
#                 return error_response
#             finally:
#                 try:
#                     self.db_utils.remove_session()
#                 except Exception as remove_error:
#                     print(f"Failed to rollback transaction: {remove_error}")
#         else:
#             try:
#                 wldw_sub = select(ActualController.NAME).where(ActualController.GSMC == company).distinct()
#                 wldw_list = select(ActualController.GSMC).where(ActualController.NAME.in_(wldw_sub))
#                 comm_fk_query = select(fklist.FKZHMC,
#                                        fklist.SKZHMC,
#                                        func.sum(fklist.FKJE).label('total') \
#                                        .where(fklist.SKZHMC.in_(wldw_list)) \
#                                        .group_by(fklist.SKZHMC, fklist.FKZHMC)
#                                        )
#                 comm_sk_query = select(sklist.FKZHMC,
#                                        sklist.SKZHMC,
#                                        func.sum(sklist.FKJE).label('total') \
#                                        .where(sklist.FKZHMC.in_(wldw_list)) \
#                                        .group_by(sklist.FKZHMC, sklist.SKZHMC)
#                                        )
#             except exc.SQLAlchemyError as e:
#                 error_response = [{'error': str(e)}]
#                 try:
#                     self.db_utils.rollback()
#                 except Exception as rollback_error:
#                     print(f"Failed to rollback transaction: {rollback_error}")
#                 return error_response
#             finally:
#                 try:
#                     self.db_utils.remove_session()
#                 except Exception as remove_error:
#                     print(f"Failed to rollback transaction: {remove_error}")
#
#
#
#
